package com.ruoyi.forum.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.forum.mapper.ForumSpecialtyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.forum.domain.ForumSpecialty;
import com.ruoyi.forum.mapper.ForumCollegeMapper;
import com.ruoyi.forum.domain.ForumCollege;
import com.ruoyi.forum.service.IForumCollegeService;

/**
 * 学院管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@Service
public class ForumCollegeServiceImpl implements IForumCollegeService 
{
    @Autowired
    private ForumCollegeMapper forumCollegeMapper;

    @Autowired
    private ForumSpecialtyMapper forumSpecialtyMapper;

    /**
     * 查询学院管理
     * 
     * @param id 学院管理主键
     * @return 学院管理
     */
    @Override
    public ForumCollege selectForumCollegeById(Long id)
    {
        ForumCollege forumCollege = forumCollegeMapper.selectForumCollegeById(id);
        ForumSpecialty forumSpecialty = new ForumSpecialty();
        forumSpecialty.setCollegeId(forumCollege.getId());
        forumSpecialty.setDelFlag("0");
        forumCollege.setForumSpecialtyList(forumSpecialtyMapper.selectForumSpecialtyList(forumSpecialty));
        return forumCollege;
    }

    /**
     * 查询学院管理列表
     * 
     * @param forumCollege 学院管理
     * @return 学院管理
     */
    @Override
    public List<ForumCollege> selectForumCollegeList(ForumCollege forumCollege)
    {
        return forumCollegeMapper.selectForumCollegeList(forumCollege);
    }

    /**
     * 新增学院管理
     * 
     * @param forumCollege 学院管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertForumCollege(ForumCollege forumCollege)
    {
        forumCollege.setCreateTime(DateUtils.getNowDate());
        int rows = forumCollegeMapper.insertForumCollege(forumCollege);
        insertForumSpecialty(forumCollege);
        return rows;
    }

    /**
     * 修改学院管理
     * 
     * @param forumCollege 学院管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateForumCollege(ForumCollege forumCollege)
    {
        forumCollege.setUpdateTime(DateUtils.getNowDate());
        forumCollegeMapper.deleteForumSpecialtyByCollegeId(forumCollege.getId());
        insertForumSpecialty(forumCollege);
        return forumCollegeMapper.updateForumCollege(forumCollege);
    }

    /**
     * 批量删除学院管理
     * 
     * @param ids 需要删除的学院管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumCollegeByIds(Long[] ids)
    {
        forumCollegeMapper.deleteForumSpecialtyByCollegeIds(ids);
        return forumCollegeMapper.deleteForumCollegeByIds(ids);
    }

    /**
     * 删除学院管理信息
     * 
     * @param id 学院管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumCollegeById(Long id)
    {
        forumCollegeMapper.deleteForumSpecialtyByCollegeId(id);
        return forumCollegeMapper.deleteForumCollegeById(id);
    }

    /**
     * 新增专业管理信息
     * 
     * @param forumCollege 学院管理对象
     */
    public void insertForumSpecialty(ForumCollege forumCollege)
    {
        List<ForumSpecialty> forumSpecialtyList = forumCollege.getForumSpecialtyList();
        Long id = forumCollege.getId();
        if (StringUtils.isNotNull(forumSpecialtyList))
        {
            List<ForumSpecialty> list = new ArrayList<ForumSpecialty>();
            for (ForumSpecialty forumSpecialty : forumSpecialtyList)
            {
                forumSpecialty.setCollegeId(id);
                if(!StringUtils.isNotNull(forumSpecialty.getDelFlag())){
                    forumSpecialty.setDelFlag("0");
                }
                list.add(forumSpecialty);
            }
            if (list.size() > 0)
            {
                forumCollegeMapper.batchForumSpecialty(list);
            }
        }
    }
}
